package org.tinygame.herostory.cmdhandler;

import io.netty.channel.ChannelHandlerContext;
import io.netty.util.AttributeKey;
import org.tinygame.herostory.Broadcaster;
import org.tinygame.herostory.model.User;
import org.tinygame.herostory.model.UserManager;
import org.tinygame.herostory.msg.GameMsgProtocol;

/**
 * @author anan
 * @create 2022-05-01 23:53
 */
public class UserEntryCmdHandler implements ICmdHandler<GameMsgProtocol.UserEntryCmd>{
    @Override
    public void handle(ChannelHandlerContext ctx, GameMsgProtocol.UserEntryCmd cmd) {
        if(null == ctx || null == cmd) return;

/*        int userId = cmd.getUserId();
        String heroAvatar = cmd.getHeroAvatar();*/
        Integer userId = (Integer) ctx.channel().attr(AttributeKey.valueOf("userId")).get();
        if(null == userId){
            return;
        }
        User existUser = UserManager.getByUserId(userId);

        GameMsgProtocol.UserEntryResult.Builder resultBuilder = GameMsgProtocol.UserEntryResult.newBuilder();
        resultBuilder.setUserId(userId);
        resultBuilder.setUserName(existUser.userName);
        resultBuilder.setHeroAvatar(existUser.heroAvatar);

        GameMsgProtocol.UserEntryResult newResult = resultBuilder.build();
        Broadcaster.broadcast(newResult);
    }
   /* public void handle(ChannelHandlerContext channelHandlerContext, GameMsgProtocol.UserEntryCmd cmd) {
        if(null == channelHandlerContext || null == cmd) return;
        int userId = cmd.getUserId();
        String heroAvatar = cmd.getHeroAvatar();

        User user = new User();
        user.userId = userId;
        user.heroAvatar = heroAvatar;
        user.currHp = 100;
        UserManager.addUser(user);

        //channelHandlerContext.channel().attr(AttributeKey.valueOf("userId")).set(userId);

        channelHandlerContext.channel().attr(AttributeKey.valueOf("userId")).set(userId);

        GameMsgProtocol.UserEntryResult.Builder resultBuilder = GameMsgProtocol.UserEntryResult.newBuilder();
        resultBuilder.setUserId(userId);
        resultBuilder.setHeroAvatar(heroAvatar);

        GameMsgProtocol.UserEntryResult newResult = resultBuilder.build();
        Broadcaster.broadcast(newResult);
    }*/


}